package com.weibo.host.severlet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.weibo.common.CommonUtil;
import com.weibo.host.service.HostService;
import com.weibo.sql.SqlFile;
import com.weibo.user.User;

public class HostUtil {
	/**
	 * 获取查询条件
	 * @param request
	 * @return
	 */
	public static SearchParm getSearchParm(HttpServletRequest request){
		SearchParm  searchParm = new SearchParm();
		searchParm.setCity(CommonUtil.parseUrlZH(request.getParameter("city")));
		searchParm.setFensi_end(request.getParameter("fensi_end")+"");
		searchParm.setFensi_start(request.getParameter("fensi_start")+"");
		searchParm.setHost_name(CommonUtil.parseUrlZH(request.getParameter("host_name")));
		searchParm.setIsv(request.getParameter("isv")+"");
		searchParm.setKey_name(CommonUtil.parseUrlZH(request.getParameter("key_name")));
		searchParm.setPlatform(CommonUtil.parseUrlZH(request.getParameter("platform")));
		searchParm.setPrice(CommonUtil.parseUrlZH(request.getParameter("price")));
		searchParm.setPrice_end(request.getParameter("price_end")+"");
		searchParm.setPrice_start(request.getParameter("price_start")+"");
		searchParm.setProvince(CommonUtil.parseUrlZH(request.getParameter("province")));
		searchParm.setSort_(CommonUtil.parseUrlZH(request.getParameter("sort_")));
		return searchParm;
	}
	/**
	 * 获取查询列表
	 * @param searchParm 查询条件
	 * @param hostService 服务类
	 * @param type 类型
	 * @param currentPage 当前页
	 * @param user 当前登录用户
	 * @return
	 */
	public static List getListByParm(SearchParm searchParm,HostService hostService,String type,int currentPage,User user){
		List list = hostService.getHostListBySql(CommonUtil.getPageSql(getSqlByParm(searchParm,type,user), currentPage));
		return list;
	}
	/**
	 * 获取当前查询的总记录条数
	 * @param searchParm
	 * @param hostService
	 * @param type
	 * @param user
	 * @return
	 */
	public static int getTotalPageNumByParm(SearchParm searchParm,HostService hostService,String type,User user){
		int totalPageNum = 0;
		totalPageNum = hostService.getHostTotalSizeBysql(CommonUtil.getTotalSizeSql_bySql(getSqlByParm(searchParm,type,user)));
		return totalPageNum;
	}
	public static String getSqlByParm(SearchParm searchParm,String type,User user){
		String sql_parm = "";
		if(type.equals("查看微博博主")){
			sql_parm = SqlFile.HOST_VISITOR_DEAFAULT;
		}else if(type.equals("红人博主")){
			sql_parm = SqlFile.HOST_HONGREN_LIST;
		}else if(type.equals("名人博主")){
			sql_parm = SqlFile.HOST_MINGREN_LIST;
		}else if(type.equals("热门微博主")){
			sql_parm = SqlFile.HOST_REMEN_LIST;
		}else if(type.equals("重点推荐")){
			sql_parm = SqlFile.HOST_ZHONGDIANTUIJIAN_LIST;
		}else if(type.equals("查看收藏夹")){
			sql_parm = CommonUtil.getSqlBy_par(SqlFile.FAVORITE_SELECT, user.getUser_id());
		}else if(type.equals("查看历史")){
			sql_parm =  CommonUtil.getSqlBy_par(SqlFile.HISTORY_SELECT, user.getUser_id());
		}else if(type.equals("查看黑名单")){
			sql_parm = CommonUtil.getSqlBy_par(SqlFile.BLACKLIST_SEARCH, user.getUser_id());
		}
		String temp = searchParm.getSort_();
		if(!(temp.equals("null")||type.equals("查看微博博主"))){
			if(temp.equals("按粉丝量从高到低")){
				sql_parm+=" ORDER BY TO_NUMBER(REPLACE( OTHERTALE.HOST_FS_NUMBER, '万', '0000')) DESC";
			}else if(temp.equals("按粉丝量从低到高")){
				sql_parm+=" ORDER BY TO_NUMBER(REPLACE( OTHERTALE.HOST_FS_NUMBER, '万', '0000')) ASC";
			}else if(temp.equals("按转评值从低到高")){
				sql_parm+=" ORDER BY OTHERTALE.HOST_ZP_VALUE ASC";
			}else if(temp.equals("按转评值从高到低")){
				sql_parm+=" ORDER BY OTHERTALE.HOST_ZP_VALUE DESC";
			}else if(temp.equals("按月订单量从低到高")){
				sql_parm+="";
			}
			else if(temp.equals("按月订单量从高到低")){
				sql_parm+="";
			}
			else if(temp.equals("按周订单量从低到高")){
				sql_parm+="";
			}
			else if(temp.equals("按周订单量从高到低")){
				sql_parm+="";
			}
			else if(temp.equals("按转发价从低到高")){
				sql_parm+=" ORDER BY OTHERTALE.PERSON_ZHUANF_PRICE DESC";
			}
			else if(temp.equals("按转发价从高到低")){
				sql_parm+=" ORDER BY OTHERTALE.PERSON_ZHUANF_PRICE ASC";
			}
			else if(temp.equals("按直发价从低到高")){
				sql_parm+=" ORDER BY OTHERTALE.PERSON_ZHIF_PRICE DESC";
			}
			else if(temp.equals("按直发价从高到低")){
				sql_parm+=" ORDER BY OTHERTALE.PERSON_ZHIF_PRICE ASC";
			}
		}else if(!temp.equals("null")&&type.equals("查看微博博主")){
			if(temp.equals("按粉丝量从高到低")){
				sql_parm+=" ORDER BY TO_NUMBER(REPLACE( HOST.HOST_FS_NUMBER, '万', '0000')) DESC";
			}else if(temp.equals("按粉丝量从低到高")){
				sql_parm+=" ORDER BY TO_NUMBER(REPLACE( HOST.HOST_FS_NUMBER, '万', '0000')) ASC";
			}else if(temp.equals("按转评值从低到高")){
				sql_parm+=" ORDER BY PRO.HOST_ZP_VALUE ASC";
			}else if(temp.equals("按转评值从高到低")){
				sql_parm+=" ORDER BY PRO.HOST_ZP_VALUE DESC";
			}else if(temp.equals("按月订单量从低到高")){
				sql_parm+="";
			}
			else if(temp.equals("按月订单量从高到低")){
				sql_parm+="";
			}
			else if(temp.equals("按周订单量从低到高")){
				sql_parm+="";
			}
			else if(temp.equals("按周订单量从高到低")){
				sql_parm+="";
			}
			else if(temp.equals("按转发价从低到高")){
				sql_parm+=" ORDER BY PRO.PERSON_ZHUANF_PRICE DESC";
			}
			else if(temp.equals("按转发价从高到低")){
				sql_parm+=" ORDER BY PRO.PERSON_ZHUANF_PRICE ASC";
			}
			else if(temp.equals("按直发价从低到高")){
				sql_parm+=" ORDER BY PRO.PERSON_ZHIF_PRICE DESC";
			}
			else if(temp.equals("按直发价从高到低")){
				sql_parm+=" ORDER BY PRO.PERSON_ZHIF_PRICE ASC";
			}
		}
		
		return sql_parm;
	}
	
	public static void main(String args[]){
		System.out.println((null+"").equals(""));
	}

}
